package com.SmartCare.service.impl.message;

import com.SmartCare.common.Result;
import com.SmartCare.mapper.message.MsgBindCareHomeMapper;
import com.SmartCare.mapper.role.UserMapper;
import com.SmartCare.pojo.message.MsgBindCareHome;
import com.SmartCare.pojo.message.MsgBindElder;
import com.SmartCare.pojo.role.ElderlyInformation;
import com.SmartCare.pojo.role.User;
import com.SmartCare.service.message.MsgBindCareHomeService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 绑定养老院消息表 服务实现类
 */
@Service
public class MsgBindCareHomeServiceImpl extends ServiceImpl<MsgBindCareHomeMapper, MsgBindCareHome> implements MsgBindCareHomeService {

    @Resource
    private UserMapper userMapper;

    @Override
    public Result query(String name) {
        QueryWrapper<MsgBindCareHome> qw = new QueryWrapper<>();
        qw.eq("care_home_name", name);
        qw.orderByDesc("create_time");
        return Result.success(list(qw));
    }

    @Override
    public Result updateMsg(MsgBindCareHome msg) {
        //更新消息状态
        updateById(msg);
        //绑定养老院
        if (msg.getStatus() == 1) {
            User user = userMapper.selectById(msg.getUserId());
            user.setCareHomeName(msg.getCareHomeName());
            userMapper.updateById(user);
        }
        return Result.success();
    }

    @Override
    public Result addNewMsg(String careHomeName, Long userId) {
        User user = userMapper.selectById(userId);
        if (user == null) {
            return Result.error("用户不存在");
        }

        MsgBindCareHome msg = MsgBindCareHome.builder()
                .userId(userId)
                .careHomeName(careHomeName)
                .build();
        save(msg);
        return Result.success();
    }
}
